home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / newmarch.zip / ONEWINDO.C < prev    next >
C/C++ Source or Header  |  1992-09-08  |  5KB  |  214 lines

  1. /* Author:   $Author: jan $
  2.  * File:     $Source: /usr/usrs/jan/desktop/X_Book.boo/programs/RCS/onewindow.c,v $
  3.  * Date:     $Date: 1992/09/09 00:10:05 $
  4.  * Revision: $Revision: 1.1 $
  5.  */
  6.  
  7. #include "copyright.h"
  8.  
  9. /*     
  10. ** File: onewindow.c
  11. ** Generic program schema for a one window program    
  12. */ 
  13.     
  14. #include <stdio.h>    
  15. #include <X11/Xlib.h>    
  16. #include <X11/Xutil.h>    
  17.     
  18. /*    
  19. ** debug lists all events to an 
  20. ** xterm window as they occur
  21. */    
  22. #define DEBUG    
  23.     
  24. char WINDOW_NAME[] = "Window";    
  25. char ICON_NAME[] = "Icon";    
  26.     
  27. Display *display;  /* the display device */    
  28. int     screen;    /* the screen on the display */    
  29. Window  main_window;    
  30. GC    gc;    
  31. unsigned long  foreground, background;    
  32.     
  33. /*    
  34. ** 
  35. */    
  36.     
  37. Window    
  38. openWindow (x, y, width, height, border_width,
  39.             argc, argv)    
  40.     int x, y;  /* coords of the upper left 
  41.                   corner in pixels */
  42.     int width,    
  43.         height;  /* size of the window in pixels */    
  44.     int border_width;  /* the border width is 
  45.                           not included in the    
  46.                           other dimensions */    
  47.     int argc;    
  48.     char **argv;    
  49. {    
  50.     Window  new_window;    
  51.     XSizeHints  size_hints;    
  52.         
  53.     /* now create the window */    
  54.     new_window = XCreateSimpleWindow (display,     
  55.                         DefaultRootWindow (display),    
  56.                         x, y, width, height,    
  57.                         border_width,    
  58.                         foreground, background);    
  59.             
  60.     /* set up the size hints for the window manager */
  61.     size_hints.x = x;    
  62.     size_hints.y = y;    
  63.     size_hints.width = width;    
  64.     size_hints.height = height;    
  65.     /* and state what hints are included */    
  66.     size_hints.flags = PPosition | PSize;    
  67.     
  68.     /* let the window manager know about the window */
  69.     XSetStandardProperties (display, new_window,    
  70.           WINDOW_NAME, ICON_NAME,    
  71.           None,      /* no icon map */    
  72.           argv, argc, &size_hints);    
  73.     
  74.     /* Decide what events the window will receive */
  75.     XSelectInput (display, new_window,    
  76.           (ButtonPressMask | KeyPressMask | 
  77.            ExposureMask));    
  78.     
  79.     /* Return the window ID */    
  80.     return (new_window);    
  81. }    
  82.     
  83. /*    
  84. ** 
  85. */    
  86. GC
  87. getGC ()    
  88. {   GC gc;
  89.     XGCValues gcValues;    
  90.     
  91.     gc = XCreateGC (display, main_window,     
  92.                 (unsigned long) 0, &gcValues);    
  93.     
  94.     XSetBackground (display, gc, background);    
  95.     XSetForeground (display, gc, foreground);    
  96.  
  97.     return (gc);
  98. }    
  99.     
  100. /*    
  101. ** Terminate the program gracefully    
  102. */    
  103. quitX ()    
  104. {    
  105.     XCloseDisplay (display);    
  106.     exit (0);    
  107. }    
  108.     
  109.     
  110. /* 
  111. ** An expose event occurs when the contents of
  112. ** a window are invalidated and at least some
  113. ** of it needs to be redrawn 
  114. */ 
  115. void    
  116. doExposeEvent (pEvent)    
  117.     XExposeEvent *pEvent;    
  118. {    
  119.     XDrawImageString (display, main_window, gc,    
  120.           10, 10, "Press q or any button to quit",    
  121.           strlen ("Press q or any button to quit"));    
  122. }    
  123.     
  124. /* 
  125. ** A button has been pressed within the window - quit
  126. */ 
  127. void doButtonPressEvent (pEvent)    
  128.     XButtonEvent *pEvent;    
  129. {    
  130.     quitX ();    
  131. }    
  132.     
  133. /* 
  134. ** A key has been pressed. 
  135. ** It needs to be decoded and acted on.
  136. ** Quit if it is a 'q'.
  137. */ 
  138. void    
  139. doKeyPressEvent (pEvent)    
  140.     XKeyEvent *pEvent;    
  141. {   int key_buffer_size = 10;    
  142.     char key_buffer[9];    
  143.     XComposeStatus compose_status;    
  144.     KeySym key_sym;    
  145.     
  146.     XLookupString (pEvent, key_buffer, 
  147.           key_buffer_size,    
  148.           &key_sym, &compose_status);    
  149.     if (key_buffer[0] == 'q')    
  150.           quitX ();    
  151. }    
  152.     
  153. void
  154. initX ()    
  155. {    
  156.     /* set the display name from the 
  157.     ** environment vbl DISPLAY */    
  158.     display = XOpenDisplay (NULL);    
  159.     if  (display == NULL)    
  160.     {      fprintf (stderr, 
  161.                     "Unable to open display %s\n",    
  162.                     XDisplayName (NULL));    
  163.           exit (1);    
  164.     }    
  165.     screen = DefaultScreen (display);    
  166.     /* use the default foreground and 
  167.     ** background colors 
  168.     */    
  169.     foreground = BlackPixel (display, screen);    
  170.     background = WhitePixel (display, screen);    
  171.     
  172. }    
  173.     
  174. main (argc, argv)    
  175.     int argc;    
  176.     char **argv;    
  177. {   XEvent event;    
  178.     
  179.     initX ();    
  180.     
  181.     main_window = openWindow (10, 20, 200, 100, 5, 
  182.                               argc, argv);    
  183.     gc = getGC ();    
  184.     
  185.     /* Display the window on the screen */    
  186.     XMapWindow (display, main_window);    
  187.     XFlush (display);    
  188.     while (True)    
  189.     {    
  190.           XNextEvent (display, &event);    
  191. #ifdef DEBUG    
  192.           printf ("Event number is %d\n", event.type);
  193. #endif    
  194.           switch  (event.type)    
  195.           {    
  196.           case Expose:  
  197.                       doExposeEvent (&event);    
  198.                       break;    
  199.     
  200.           case ButtonPress:    
  201.                       doButtonPressEvent (&event);    
  202.                       break;    
  203.     
  204.           case KeyPress:
  205.                       doKeyPressEvent (&event);    
  206.                       break;    
  207.     
  208.           case MappingNotify:    
  209.                       XRefreshKeyboardMapping (&event);
  210.                       break;    
  211.           }    
  212.     }    
  213. }
  214.